﻿using EMoney.IStock.Client.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace EMoney.IStock.Client.Business
{
    /// <summary>
    /// 交易日
    /// </summary>
    public class TradeDateBll
    {
        private static List<DateTime> lst = new List<DateTime>();

        /// <summary>
        /// 判断传入的日期是否为交易日
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsTradeDate(DateTime date)
        {
            if (lst.Count == 0)
            {
                string notTradeDate = ConfigHelper.NotTradeDate;

                if (!string.IsNullOrEmpty(notTradeDate))
                {
                    string[] arrNotTradeDate = notTradeDate.Split(',');

                    if (arrNotTradeDate != null && arrNotTradeDate.Length > 0)
                    {
                        DateTime dtDate = DateTime.MinValue;

                        foreach (string str in arrNotTradeDate)
                        {
                            if (DateTime.TryParse(str, out dtDate))
                            {
                                lst.Add(dtDate);
                            }
                        }
                    }
                }                
            }

            date = date.Date;
            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
            {
                return false;
            }
            else
            {
                return !lst.Any(x => x == date);
            }
        }

        /// <summary>
        /// 获取下一交易日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetNextTradeDate(DateTime dt)
        {
            dt = dt.AddDays(1);

            if (IsTradeDate(dt.Date))
            {
                return dt.Date;
            }
            else
            {
                return GetNextTradeDate(dt);
            }
        }

        /// <summary>
        /// 获取上一交易日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetPreTradeDate(DateTime dt)
        {
            dt = dt.AddDays(-1);

            if (IsTradeDate(dt.Date))
            {
                return dt.Date;
            }
            else
            {
                return GetPreTradeDate(dt);
            }
        }

        /// <summary>
        /// 获取当前交易日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetTradeDate()
        {
            DateTime dtNow = DateTime.Now;

            if (IsTradeDate(dtNow))
            {
                return dtNow;
            }
            else
            {
                return GetPreTradeDate(dtNow);
            }
        }
    }
}
